<template>
  <div style="padding: 0 20px;">
    <el-row>
      <el-col :span="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>填写假条</span>
          </div>
          <el-select v-model="leaveType" clearable placeholder="请选择请假类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <div style="height:20px"></div>
          <el-date-picker
            value-format="yyyy-MM-dd hh:mm:ss"
            class="picker"
            v-model="timeData"
            type="datetimerange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
          <div style="height:20px"></div>
          <el-input
            type="textarea"
            placeholder="请输入请假原因"
            v-model="textarea"
            maxlength="100"
            show-word-limit
          ></el-input>
          <div style="height:20px"></div>
          <div style="float: right">
            <el-button type="primary" @click="onApplication">提交</el-button>
            <div style="height:20px"></div>
          </div>
        </el-card>

        <el-card style="margin-top:20px ;margin-bottom: 5rem">
          <el-table
            :data="tableData"
            border
            :default-sort="{prop: 'createtime', order: 'descending'}"
            style="width: 100%"
          >
            <el-table-column align="center" width="160px" label="申请时间" sortable>
              <template slot-scope="scope">{{ scope.row.createtime}}</template>
            </el-table-column>
            <el-table-column align="center" min-width="90px" prop="leavetype" label="类型"></el-table-column>
            <el-table-column align="center" min-width="200px" prop="reason" label="内容"></el-table-column>

            <el-table-column align="center" min-width="160px" prop="starttime" label="请假开始时间"></el-table-column>

            <el-table-column align="center" min-width="160px" prop="endtime" label="请假结束时间"></el-table-column>

            <el-table-column min-width="130px" label="审核结果" align="center">
              <template slot-scope="scope">
                <el-tag :type="scope.row.state | tagFilter">{{ scope.row.state | stateFilter}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" min-width="90px" label="审核人ID">
              <template slot-scope="scope">
                <span>{{scope.row.t_id ? scope.row.t_id : '暂无审核'}}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" min-width="120px" label="审核人名称">
              <template slot-scope="scope">
                <span>{{scope.row.t_name ? scope.row.t_name : '暂无审核'}}</span>
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="text-align:center;margin-top:20px">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 15, 20,25]"
              :page-size="5"
              layout="total, sizes, prev, pager, next, jumper"
              :total="UserSize"
            ></el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { setLeave, getUserLeave } from "../../api/student-api/application";
export default {
  name: "application",
  data() {
    return {
      UserSize: 0,
      currentPage: 0,
      tableData: [],
      pageSize: 5,
      pageNo: 1,
      currentPage1: 1,
      text: "",
      textarea: "",
      options: [
        {
          value: "外出",
          label: "外出"
        },
        {
          value: "身体不适",
          label: "身体不适"
        },
        {
          value: "其他(在请假原因里说明)",
          label: "其他(在请假原因里说明)"
        }
      ],
      leaveType: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      timeData: ""
    };
  },
  created() {
    this.getApplication(this.pageNo, this.pageSize);
  },

  filters: {
    stateFilter(type) {
      if (type == 0) {
        return "未审批";
      }
      if (type == 1) {
        return "审批不通过";
      }
      if (type == 2) {
        return "审批通过";
      }
    },
    tagFilter(type) {
      const typeOptions = {
        0: "info",
        1: "warning",
        2: "success"
      };
      return typeOptions[type];
    }
  },
  methods: {
    /**获取我的请假单
     */
    getApplication(pageNo, pageSize) {
      getUserLeave(pageNo, pageSize).then(res => {
        this.tableData = res.data.data;
        this.UserSize = res.data.total;
      });
    },

    /**
     * 请假
     */
    onApplication() {
      if (!this.textarea || !this.leaveType || !this.timeData)
        this.open3("请假信息不可以留空");
      else {
        setLeave(
          this.textarea,
          this.leaveType,
          this.timeData[0],
          this.timeData[1]
        ).then(res => {
          this.$message({
            message: "请假申请填写成功，请耐心等待审批",
            type: "success"
          });
          this.getApplication(this.pageNo, this.pageSize);
          this.textarea = "";
          this.leaveType = "";
          this.timeData = "";
        });
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getApplication(this.pageNo, this.pageSize);
    },
    handleCurrentChange(val) {
      // console.log(`当前页${val} `);
      this.pageNo = val;
      this.getApplication(this.pageNo, this.pageSize);
    },
    open3(v) {
      this.$message({
        message: v,
        type: "warning"
      });
    }
  }
};
</script>

<style lang="scss">
.picker {
  .el-range-separator {
    box-sizing: content-box;
  }
}
</style>
